/*
 * @Author: silence
 * @Date: 2023-09-28 15:05:08
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-10-09 16:51:40
 * @Description: 思道尔设备配网流程
 */
import 'dart:convert' as convert;

import 'package:dio/dio.dart';
import 'package:ems_neutral_app/pages/DeviceNetworking/Interface/WifiDeviceBackData.dart';
import 'package:sil_utils_package/log/SilLog.dart';

import 'DeviceNetworkingInterface.dart';

class DeviceNetworkingInterfaceForSDER extends DeviceNetworkingInterface {
  static String SDER_URL_BASE = 'http://10.10.10.1/';
  Dio dio = Dio();

  /// 获取设备信息
  @override
  Future<WifiDeviceBackData> checkDeiviceWifiConnectState() async {
    WifiDeviceBackData resultDeviceData =
        WifiDeviceBackData(code: '-1', data: '-1');

    try {
      var result = await dio.getUri(Uri.parse(SDER_URL_BASE + 'getdev.cgi'),
          options: Options(sendTimeout: Duration(seconds: 5)));
      SilLog.i(result.data);

      Map<String, dynamic>? response;
      if (result.data is String) {
        response = convert.jsonDecode(result.data);
        SilLog.e(response);
      } else {
        response = result.data as Map<String, dynamic>;
      }
      resultDeviceData.code = '1';
      resultDeviceData.data = response?['sn'];
      resultDeviceData.msg = 'success';
    } catch (e) {
    } finally {
      return resultDeviceData;
    }
  }

  @override
  Future<WifiDeviceBackData> deiviceSetHomeWifi(
      {required String ssid, required String pwd}) async {
    WifiDeviceBackData resultDeviceData =
        WifiDeviceBackData(code: '-1', data: '-1');

    Map parm = {"ssid": ssid, "pwd": pwd};
    try {
      var result = await dio.postUri(Uri.parse(SDER_URL_BASE + 'staset.cgi'),
          data: convert.jsonEncode(parm),
          options: Options(
              sendTimeout: Duration(seconds: 5),
              headers: {"Content-Type": "application/x-www-form-urlencoded"}));
      SilLog.i(result.data);
      Map<String, dynamic>? response;
      if (result.data is String) {
        response = convert.jsonDecode(result.data);
        SilLog.e(response);
      } else {
        response = result.data as Map<String, dynamic>;
      }

      if (response != null && response.containsKey('status')) {
        if (response['status'] == 'ok') {
          resultDeviceData.code = '1';
          resultDeviceData.data = response['status'];
          resultDeviceData.msg = 'success';
        }
      }
    } catch (e) {
      SilLog.i(e);
    } finally {
      return resultDeviceData;
    }
  }

  @override
  Future<WifiDeviceBackData> deiviceRestart() async {
    WifiDeviceBackData resultDeviceData =
        WifiDeviceBackData(code: '-1', data: '-1');

    resultDeviceData.code = '1';
    resultDeviceData.data = '';
    resultDeviceData.msg = 'success';
    return resultDeviceData;
  }

  @override
  Future<WifiDeviceBackData> checkIsSuccess() async {
    WifiDeviceBackData resultDeviceData =
        WifiDeviceBackData(code: '-1', data: '-1');

    var response;
    try {
      response = await dio.getUri(Uri.parse(SDER_URL_BASE + 'stainfo.cgi'),
          options: Options(sendTimeout: Duration(seconds: 5)));
      resultDeviceData.code = response['sta'];
      resultDeviceData.data = response['ssid'];
      resultDeviceData.msg = response['success'];
    } catch (e) {}

    return resultDeviceData;
  }
}
